﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;

namespace RemoteMeter
{
    public class DataManager
    {
        DataTable dt;

        string[] paramsNames;
        string str_duration = "Mean Duration";
        string str_durationVar = "Duration Variance";
        string[] ammetersNames;
        int tests=1;

        public DataManager(string algName, string[] paramsNames, string[] ammetersNames, int tests)
        {
            this.tests = tests;
            this.paramsNames = paramsNames;
            this.ammetersNames = ammetersNames;
            dt = new DataTable(algName);
            for (int i = 0; i < paramsNames.Length; i++)
            {
                dt.Columns.Add(paramsNames[i], typeof(string));
            }
            for (int j = 0; j < this.tests; j++)
            {
                dt.Columns.Add(str_duration + " " + (j+1), typeof(double));
            }
            //dt.Columns.Add(str_durationVar, typeof(double));
            for (int i = 0; i < ammetersNames.Length; i++)
            {
                for (int j = 0; j < this.tests; j++)
                {
                    dt.Columns.Add(ammetersNames[i]+" " + (j+1), typeof(double));
                }
            }
        }

        public void AddTest(object[] parameters, List<double> duration, List<double []> ammeters)
        {
            DataRow newrow = dt.NewRow();
            for (int i = 0; i < parameters.Length; i++)
            {
                newrow[paramsNames[i]] = parameters[i].ToString();
            }
            for (int j = 0; j < this.tests; j++)
            {
                newrow[str_duration + " " + (j+1)] = duration[j];
            }
            //newrow[str_durationVar] = durVariance;
            for (int j = 0; j < tests; j++)
            {
                for (int i = 0; i < ammeters[j].Length; i++)
                {
                    newrow[ammetersNames[i]+" " + (j+1)] = ammeters[j][i].ToString();
                }
            }
            dt.Rows.Add(newrow);
        }

        public void SaveData(string filepath)
        {
            dt.WriteXml(filepath);
        }
    }
}
